const reqInterface = require("../../../../../api/models");
Page({
  data: {
    activeNames: ["1"],
    descriptionStorage: '',
    description: '',
    id: '',

    keyboardHeight: 0,
    isIOS: false,
    formats: {}
  },


  onLoad(options) {
    this.setData({
      id: options.id
    })

    const platform = wx.getSystemInfoSync().platform
    const isIOS = platform === 'ios'
    this.setData({
      isIOS
    })
    const self = this
    this.updatePosition(0)
    let keyboardHeight = 0
    wx.onKeyboardHeightChange(res => {
      if (res.height === keyboardHeight) return
      const duration = res.height > 0 ? res.duration * 1000 : 0
      keyboardHeight = res.height
      setTimeout(() => {
        wx.pageScrollTo({
          scrollTop: 0,
          success() {
            self.updatePosition(keyboardHeight)
            self.editorCtx.scrollIntoView()
          }
        })
      }, duration)

    })
  },

  onShow() {
    let self = this;
    if(self.data.id) {
      reqInterface.GetJobapplicantIntroductiondetail({
        id: self.data.id
      }).then(res => {
        self.setData({
          description: res.description
        })
      })
    }

  },

  // 公告内容
  onEditorContentReady() {
    const self = this
    wx.createSelectorQuery().select('#editorContent').context(function (res) {
      self.editorCtx = res.context
    }).exec()
  },
  onEditorContentInput(e) {
    var self = this;
    self.setData({
      descriptionStorage: e.detail.html.replace(/wx:nodeid="\d+"/g, '')
    })
    console.log(self.data.descriptionStorage)
  },

  send(e) {
    var self = this;
    console.log(self.data.descriptionStorage)
    let params = {
      description: self.data.description ? self.data.description +  self.data.descriptionStorage : self.data.descriptionStorage
    }
    if (self.data.descriptionStorage) {
      reqInterface.PostJobapplicantAddintroduction(params).then(res => {
        wx.showToast({
          title: '编辑成功',
          icon: 'success'
        });
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '未填写个人简介',
      })
    }
  },


  updatePosition(keyboardHeight) {
    const toolbarHeight = 148;
    const {
      windowHeight,
      platform
    } = wx.getSystemInfoSync()
    let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
    this.setData({
      editorHeight,
      keyboardHeight
    })
  },
  calNavigationBarAndStatusBar() {
    const systemInfo = wx.getSystemInfoSync()
    const {
      statusBarHeight,
      platform
    } = systemInfo
    const isIOS = platform === 'ios'
    const navigationBarHeight = isIOS ? 44 : 48
    return statusBarHeight + navigationBarHeight
  },
  onStatusChange(e) {
    const formats = e.detail
    this.setData({
      formats
    })
  },
  format(e) {
    let {
      name,
      value
    } = e.target.dataset
    if (!name) return
    // console.log('format', name, value)
    this.editorCtx.format(name, value)

  },
})